package com.ruoyi.api;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;

// 假设 AlipayConfig 类存在，如果不存在需要添加其定义
class AlipayConfig {
    private String serverUrl;
    private String appId;
    private String privateKey;
    private String format;
    private String alipayPublicKey;
    private String charset;
    private String signType;

    // Getter 和 Setter 方法
    public String getServerUrl() {
        return serverUrl;
    }

    public void setServerUrl(String serverUrl) {
        this.serverUrl = serverUrl;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getPrivateKey() {
        return privateKey;
    }

    public void setPrivateKey(String privateKey) {
        this.privateKey = privateKey;
    }

    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public String getAlipayPublicKey() {
        return alipayPublicKey;
    }

    public void setAlipayPublicKey(String alipayPublicKey) {
        this.alipayPublicKey = alipayPublicKey;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public String getSignType() {
        return signType;
    }

    public void setSignType(String signType) {
        this.signType = signType;
    }
}

public class AlipaySystemOauthToken {
    // 假设诊断工具类存在，如果不存在需要添加其定义
    // 静态常量用于配置信息，便于修改和维护
    private static final String SERVER_URL = "https://openapi.alipay.com/gateway.do";
    private static final String FORMAT = "json";
    private static final String CHARSET = "UTF-8";
    private static final String SIGN_TYPE = "RSA2";
    private static final String PRIVATE_KEY = "<-- 请填写您的应用私钥，例如：MIIEvQIBADANB ... ... -->";
    private static final String ALIPAY_PUBLIC_KEY = "<-- 请填写您的支付宝公钥，例如：MIIBIjANBg... -->";
    private static final String APP_ID = "<-- 请填写您的AppId，例如：2019091767145019 -->";

    public static void main(String[] args) {
        try {
            // 初始化 SDK
            AlipayClient alipayClient = createAlipayClient();
            // 构造请求参数以调用接口
            AlipaySystemOauthTokenRequest request = buildRequest();
            // 执行请求
            AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
            handleResponse(response);
        } catch (Exception e) {
            // 捕获异常并输出错误信息，方便调试
            System.err.println("请求执行过程中出现异常: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 创建 AlipayClient 实例
     * @return AlipayClient 实例
     */
    private static AlipayClient createAlipayClient() {
        return new DefaultAlipayClient(
                SERVER_URL,
                APP_ID,
                PRIVATE_KEY,
                FORMAT,
                CHARSET,
                ALIPAY_PUBLIC_KEY,
                SIGN_TYPE
        );
    }

    /**
     * 构建 AlipaySystemOauthTokenRequest 请求对象
     * @return AlipaySystemOauthTokenRequest 请求对象
     */
    private static AlipaySystemOauthTokenRequest buildRequest() {
        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        // 设置刷新令牌
        request.setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");
        // 设置授权码
        request.setCode("4b203fe6c11548bcabd8da5bb087a83b");
        // 设置授权方式
        request.setGrantType("authorization_code");
        return request;
    }

    /**
     * 处理支付宝响应
     * @param response 支付宝响应对象
     */
    private static void handleResponse(AlipaySystemOauthTokenResponse response) {
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk 版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }
}
